package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysCertificate;
import com.ruoyi.system.domain.SyseEventsPosted;
import com.ruoyi.system.domain.mongodb.Schools;
import com.ruoyi.system.mongo.Impl.StudentDaoImpl;
import com.ruoyi.system.service.ISysEventsPostedService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 赛事管理
 */
@Api("赛事管理")
@RestController
@RequestMapping("/system/competition")
public class SyseEventsPostedController {
     @Autowired
    private ISysEventsPostedService competitionService;
     @Autowired
     MongoTemplate mongoTemplate;
     @Autowired
     StudentDaoImpl studentDao;
     private static final ThreadLocal<SimpleDateFormat> df =
             ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));


     @GetMapping("queryEndTimeByEpid/{epid}")
    @ApiOperation("查询当前距离赛事结束还有多少时间")
    public AjaxResult queryEndTimeByEpid(@PathVariable String epid){
        Date now = new Date();
        String endtime = competitionService.queryEndTimeByEpid(epid);
        Date endTime = DateUtils.parseDate(endtime);
        String datePoor = DateUtils.getDatePoor(endTime,now);
        if( datePoor.indexOf("-1")!=-1){
            return AjaxResult.error("操作成功","活动已结束");
        }
        return AjaxResult.success("操作成功",datePoor);
        
    }

     //根据老师id查询对应的学校
     @GetMapping("selectByIdSchool")
     public Schools queryUsers(String id){
          Schools schools = competitionService.queryUsers(id);
          return schools;
     }


    @GetMapping("/queryEventByTid/{tid}")
    @ApiOperation("教师页面我的邀请查询被邀请的赛事信息")
    public AjaxResult queryEventByTid(@PathVariable String tid,Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<SyseEventsPosted> syseEventsPosteds = competitionService.queryEventByTid(tid);
        PageInfo<SyseEventsPosted>  pageInfo=new PageInfo<>(syseEventsPosteds);
        return AjaxResult.success(pageInfo);
    }

    @GetMapping("/queryEventByTidAndEvent")
    @ApiOperation("教师页面我的邀请查询被邀请的赛事信息")
    public AjaxResult queryEventByTidAndEvent(String tid,int epid) {
        System.out.println(tid+", "+epid);
         if (epid==0){
             return AjaxResult.success(competitionService.queryEventByTid(tid));
         }else {
             return AjaxResult.success(competitionService.queryEventByTid(tid));
         }
    }


    @GetMapping("/queryAllSyseEventsPosted")
    @ApiOperation("查询所有赛事信息")
    public AjaxResult queryAllSyseEventsPosted(SyseEventsPosted syseEventsPosted) {
        return AjaxResult.success(competitionService.queryAllSyseEventsPosted(syseEventsPosted));
    }


    @GetMapping("/querySyseEventsPostedByEplevel")
    @ApiOperation("教师页面查询所有区域级赛事信息")
    public AjaxResult querySyseEventsPostedByEplevel() {
        return AjaxResult.success(competitionService.querySyseEventsPostedByEplevel());
    }

     int id;
     //获取用户id
     public  void a(Long userid){
          id= Math.toIntExact(userid);
     }

    /*
     * 添加赛事发布
     * 后台管理平台使用
     * */
     @PostMapping("insert")
     public AjaxResult insertEventsPosted(SyseEventsPosted syseEventsPosted,MultipartFile eppicfile) {
          //添加时间
          syseEventsPosted.setEpcreatetime(df.get().format(new Date()));
          //操作人
          syseEventsPosted.setEptid(String.valueOf(id));
          //上传文件(图片)
          syseEventsPosted.setFile(eppicfile);
          int i = competitionService.insertEventsPosted(syseEventsPosted);
          return AjaxResult.success(i);
     }
     /*
     * insert和insert2的区别：操作人id不同
      * 添加赛事发布(校园级)
      * 校园管理平台使用
      * */
     @PostMapping("insert2")
     public AjaxResult insertEventsPosted2(SyseEventsPosted syseEventsPosted,MultipartFile eppicfile) {
          //添加时间
          syseEventsPosted.setEpcreatetime(df.get().format(new Date()));
          //上传文件(图片)
          syseEventsPosted.setFile(eppicfile);
          int i = competitionService.insertEventsPosted(syseEventsPosted);
          return AjaxResult.success();
     }

    /**
     * 赛事管理  条件查询+分页
     * 后台管理和校园管理平台共同使用
     * @param syseEventsPosted 实体类
     * @return 对象列表
     */
    @ApiOperation("条件查询+分页")
    @ApiImplicitParam(name = "SyseEventsPosted", value = "赛事实体类", required = true,dataType = "SyseEventsPosted")
    @GetMapping("selectByPage")
    public Map<String, Object> selectByPage(SyseEventsPosted syseEventsPosted) {
         Map<String, Object> map = competitionService.selectByPage(syseEventsPosted);
        return map;
    }

    /**
     * 赛事管理  根据赛事状态进行查询
     * @param epState 赛事状态
     * @return 对象列表
     */
    @ApiOperation("根据赛事状态进行查询")
    @ApiImplicitParam(name = "epState",value = "赛事状态")
    @GetMapping("selectEpTitle")
    public List<SyseEventsPosted> selectEpTitle(@RequestParam("epState") String epState, String epschool){
        return competitionService.selectEpTitle(epState, epschool);
    }

    /**
     * 根据老师所在得学校来查询赛事
     */
    @RequestMapping("selectSsTeacherSchool")
    public List<SyseEventsPosted> selectSsTeacherSchool(String epState,String teacherId){
        return competitionService.selectSsTeacherSchool(epState,teacherId);
    }

     /**
      *  修改赛事
      *  后台管理平台使用
      * @param syseEventsPosted 实体类
      * @return 结果
      */
     @ApiOperation("修改赛事")
     @ApiImplicitParam(name = "SyseEventsPosted", value = "赛事实体类", required = true,dataType = "SyseEventsPosted")
     @PostMapping("edit")
     public AjaxResult edit(SyseEventsPosted syseEventsPosted,MultipartFile eppicfile) {
          //上传文件（图片）
          syseEventsPosted.setFile(eppicfile);
          //操作人id
          syseEventsPosted.setEptid(String.valueOf(id));
          //修改操作时间
          syseEventsPosted.setEpedittime(df.get().format(new Date()));
          int edit = competitionService.edit(syseEventsPosted);
         return AjaxResult.success();
     }
     /**
      * edit和edit2的区别：操作人id不同
      *  修改赛事（校园级）
      *  校园管理平台使用
      * @param syseEventsPosted 实体类
      * @return 结果
      */
     @ApiOperation("修改赛事")
     @ApiImplicitParam(name = "SyseEventsPosted", value = "赛事实体类", required = true,dataType = "SyseEventsPosted")
     @PostMapping("edit2")
     public AjaxResult edit2(SyseEventsPosted syseEventsPosted,MultipartFile eppicfile) {
          //上传文件（图片）
          syseEventsPosted.setFile(eppicfile);
          //修改操作时间
          syseEventsPosted.setEpedittime(df.get().format(new Date()));
          int edit = competitionService.edit(syseEventsPosted);
       return AjaxResult.success();
     }


     /**
      *  删除赛事
      *  后台管理平台和校园管理平台共同使用
      * @param epid
      * @return 结果
      */
     @ApiOperation("删除赛事")
     @ApiImplicitParam(name = "epid", value = "id", required = true,dataType = "int")
     @PostMapping("remove")
     public AjaxResult remove(int epid) {
          int i = competitionService.remove(epid);
          return  AjaxResult.success();
     }

     /**
      *  发布
      *  后台管理平台和校园管理平台共同使用
      * @param epid
      * @return 结果
      */
     @ApiOperation("发布")
     @ApiImplicitParam(name = "epid", value = "id", required = true,dataType = "int")
     @PostMapping("editState")
     public AjaxResult editState(int epid) {
          int i = competitionService.editState(epid);
        return   AjaxResult.success();
     }

     /**
      *  通过id查询对象
      *  后台管理平台和校园管理平台共同使用
      * @param epid
      * @return 对象
      */
     @ApiOperation("通过id查询对象")
     @ApiImplicitParam(name = "epid", value = "id", required = true,dataType = "int")
     @RequestMapping("selectById")
     public SyseEventsPosted selectById(int epid) {
          SyseEventsPosted syseEventsPosted = competitionService.selectById(epid);
          return syseEventsPosted;
     }


     /*
      * 查询标题
      * */
     @ApiOperation("查询标题")
     @GetMapping("selectTitle")
     public List<SyseEventsPosted> selectTitle() {
          List<SyseEventsPosted> syseEventsPosteds = competitionService.selectTitle();
          return syseEventsPosteds;
     }

     /*
      * 查询主办方
      * */
     @ApiOperation("查询主办方")
     @GetMapping("selectSponsor")
     public List<SyseEventsPosted> selectSponsor() {
          List<SyseEventsPosted> syseEventsPosteds = competitionService.selectSponsor();

          return syseEventsPosteds;
     }

     /*
      * 查询赛事状态
      * */
     @ApiOperation("查询赛事状态")
     @GetMapping("selectEpstate")
     public List<SyseEventsPosted> selectEpstate() {
          List<SyseEventsPosted> syseEventsPosteds = competitionService.selectEpstate();
          return syseEventsPosteds;
     }

     /*
      * 查询赛事级别
      * */
     @ApiOperation("查询赛事级别")
     @GetMapping("selectEplevel")
     public List<SyseEventsPosted> selectEplevel() {
          List<SyseEventsPosted> syseEventsPosteds = competitionService.selectEplevel();
          return syseEventsPosteds;
     }

    /**
     * 赛事排名分页(全部)
     * @param currentPage 当前页码
     * @param pageSize  每页行数
     * @param epLevel 赛事级别
     * @return
     */
     @RequestMapping("rankAllPage")
     public PageInfo<SyseEventsPosted> rankAllPage(@Param("currentPage") Integer currentPage, @Param("pageSize")Integer pageSize, String epLevel) {
         PageHelper.startPage(currentPage, pageSize);//分页
         List<SyseEventsPosted> list = competitionService.rankAllPage(epLevel);
         PageInfo<SyseEventsPosted> pageInfo = new PageInfo<>(list);
          return pageInfo;
     }

    //查询某个赛事下面的所有赛事状态
    @RequestMapping("selectByEpTitle")
    public List<SyseEventsPosted> selectByEpTitle(Integer epid){
        return competitionService.selectByEpTitle(epid);
    }

    @RequestMapping("selectSyseEventsPostedById")
    public SyseEventsPosted selectSyseEventsPostedById(Integer epid){
        return competitionService.selectSyseEventsPostedById(epid);
    }


}
